package cn.ihoway.game;

import cn.ihoway.redis.AllocationRedis;
import cn.ihoway.socket.GameWebSocketHandler;
import cn.ihoway.type.NotifyType;
import cn.ihoway.util.AccessRoute;
import cn.ihoway.util.HowayEncrypt;
import cn.ihoway.util.HowayLog;
import cn.ihoway.util.HowayWsResult;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
 * 分工，用于处理玩家各种请求
 */
public class Division {

    private final HowayLog logger = new HowayLog(GameWebSocketHandler.class);
    private final AllocationRedis allocationRedis = new AllocationRedis();

    public void doExecute(JSONObject req, WebSocketSession session){
        try{
            Object sessionId = session.getAttributes().get("session_id");
            System.out.println(Objects.requireNonNull(session.getRemoteAddress()).getAddress().getHostAddress());
            req.put("sessionId",sessionId);
            req.put("ip",Objects.requireNonNull(session.getRemoteAddress()).getAddress().getHostAddress());
            req.put("eventNo",HowayEncrypt.encrypt(UUID.randomUUID().toString(),"MD5",12));
            HowayWsResult rs = AccessRoute.handle(req.get("type").toString(),req);
            rs.setNotifyType(NotifyType.toEnum(req.get("type").toString()));
            session.sendMessage(new TextMessage(rs.toString()));
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static void exit(String sessionId){
        Map<String,Object> map = new HashMap<>();
        map.put("eventNo",HowayEncrypt.encrypt(UUID.randomUUID().toString(),"MD5",12));
        map.put("sessionId",sessionId);
        AccessRoute.handle("exit",map);
    }
}
